package com.qf.web.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.aop.aspect.ApiOperationLog;
import com.qf.aop.aspect.ApiOperationLogTitle;
import com.qf.api.blog.dto.NameDTO;
import com.qf.api.blog.entity.CategoryEntity;
import com.qf.api.blog.service.CategoryService;
import com.qf.api.blog.vo.OptionsVO;
import com.qf.api.blog.vo.NameVO;
import com.qf.api.sys.mybatis.page.BaseController;
import com.qf.api.sys.mybatis.page.PageResult;
import com.qf.core.enums.common.BusinessTypeEnum;
import com.qf.core.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: sin
 * @Date: 2025/5/23 16:52
 * @Description: 分类
 **/
@Tag(name = "01.分类管理")
@ApiOperationLogTitle("分类模块")
@RestController
@RequestMapping("/admin/category")
public class AdminCategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    @Operation(summary = "01.分页查询分类", description = "根据分类名称分页获取分类信息")
    @GetMapping
    public ResponseResult<PageResult<NameVO>> getCategoryPage(@RequestParam("name") String name) {
        startPage();
        List<CategoryEntity> list = categoryService.list(
                new QueryWrapper<CategoryEntity>()
                        .like("name", name)
                        .eq("deleted", 0)
                        .orderByAsc("create_time")
        );
        List<NameVO> nameVOList = BeanUtil.copyToList(list, NameVO.class);
        return ResponseResult.success(getData(nameVOList));
    }

    @Operation(summary = "02.添加分类", description = "添加分类")
    @ApiOperationLog(desc = "添加分类", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseResult<Boolean> save(@RequestBody @Validated NameDTO nameDTO) {
        return isOk(categoryService.addCategory(nameDTO));
    }

    @PutMapping("/{id}")
    @Operation(summary = "03.更新分类")
    @ApiOperationLog(desc = "更新分类",businessType = BusinessTypeEnum.MODIFY)
    public ResponseResult<Boolean> updateCategory(@PathVariable("id") @NotNull Long id, @RequestBody @Validated NameDTO nameDTO) {
        return isOk(categoryService.updateCategory(id, nameDTO));
    }

    @DeleteMapping
    @Operation(summary = "04.删除分类")
    @ApiOperationLog(desc = "删除分类", businessType = BusinessTypeEnum.DELETE)
    public ResponseResult<Boolean> deleteCategory(@RequestBody List<Long> ids) {
        // TODO 健壮性删除  后续优化
        return isOk(categoryService.removeBatchByIds(ids));
    }

    @GetMapping("/options")
    @Operation(summary = "05.获取分类下拉列表")
    public ResponseResult<List<OptionsVO>> getCategoryOptionsVO() {
        List<CategoryEntity> list = categoryService.list();
        return ResponseResult.success(BeanUtil.copyToList(list, OptionsVO.class));
    }

}
